package org.sq;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class StatusFragment extends Fragment {

	ListView listView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d("StatusFragment", "onCreateView");
		View view = inflater.inflate(R.layout.stat, container, false);
		listView = (ListView) view.findViewById(R.id.listView);
		listView.setAdapter(adapter);
		Button rBtn = (Button) view.findViewById(R.id.refreshBtn);
		rBtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				refreshAction(v);
				
			}
			
		});
		return view;
	}
	
	public void onStart(){
		super.onStart();
		if(adapter != null){
			adapter.notifyDataSetChanged();
			Log.d("StatusFragment", "onStart");
		}
		Log.d("StatusFragment", "onStart");
	}
	
	public void refreshAction(View v){
		if(adapter != null){
			adapter.notifyDataSetChanged();
			Log.d("StatusFragment", "refreshAction");
		}
	}
	
	public void onStop(){
		Log.d("StatusFragment", "onStop");
		super.onStop();
	}
	
//	@Override
//	public void onResume() {
//	    super.onResume();
//	    Handler handler = getActivity().getWindow().getDecorView().getHandler();
//	    handler.post(new Runnable() {
//	        @Override public void run() {
//	            // initializing the loader seems to cause the problem!
//	            getLoaderManager().initLoader(0, null, (LoaderCallbacks<D>) StatusFragment.this);
//	        }
//	    });
//	}
	
//	public void onResume(){
//		super.onResume();
//		if(adapter != null){
//			adapter.notifyDataSetChanged();
//			Log.d("StatusFragment", "onResume");
//		}
//		Log.d("StatusFragment", "onResume");
//	}

	@Override
	public void onAttach(Activity activity) {
		Log.d("StatusFragment", "onAttach");
		super.onAttach(activity);
	}

	BaseAdapter adapter = new BaseAdapter() {

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final LevelManager levelManager = ((SQApp) getActivity().getApplicationContext()).levelManager();
			LayoutInflater inflater = (LayoutInflater) getActivity()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			ArrayList<Quest> quests = levelManager.getQuests();
			View rowView = convertView;
			if (rowView == null) {
				rowView = inflater.inflate(R.layout.status_item, parent,
						false);
			}
			
			TextView tv1 = (TextView) rowView.findViewById(R.id.label1);
			TextView tv2 = (TextView) rowView.findViewById(R.id.label2);
			TextView tv3 = (TextView) rowView.findViewById(R.id.label3);
			TextView tv4 = (TextView) rowView.findViewById(R.id.label4);
			
			
			if(position == 0){
				tv1.setText(getResources().getString(R.string.stat_column1_title));
				tv2.setText(getResources().getString(R.string.stat_column2_title));
				tv3.setText(getResources().getString(R.string.stat_column3_title));
				tv4.setText(getResources().getString(R.string.stat_column4_title));
			} else if(position == (quests.size() +1)){
				tv1.setText(getResources().getString(R.string.summary));
				tv2.setText("");
				tv3.setText("" + levelManager.getPointsSum());
				tv4.setText("");
			} else if(position <= quests.size()){
				Quest quest = quests.get(position - 1);
				tv1.setText(quest.getLevelName());
				tv2.setText("" + quest.getUserReply());
				tv3.setText("" + quest.getPoints());
				tv4.setText(quest.getAnswerTime());
			}

			return rowView;

		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			final LevelManager levelManager = ((SQApp) getActivity().getApplicationContext()).levelManager();
			return levelManager.getQuests().size() + 2;
		}
	};

}
